﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThesisSystem.Models;
using ThesisSystem.DAL;
using ThesisSystem.ViewModels;
using PagedList;
using System.Web.Helpers;
using ThesisSystem.Helpers;
namespace ThesisSystem.Controllers
{ 
    public class MarkingsController : Controller
    {
        private ThesisSystemEntities db = new ThesisSystemEntities();

        //
        // GET: /Markings/

        public ViewResult Index()
        {
            return View(db.Markings.ToList());
        }

        //
        // GET: /Markings/Details/5

        public ViewResult Details(int id)
        {
            Marking marking = db.Markings
                .Include(t => t.Targets)
                .Where(t => t.MarkingID == id)
               .SingleOrDefault();
            
            
          
            return View(marking);
        }

        //
        // GET: /Markings/Create

        public ActionResult Create()
        {
            var thesisType = db.ThesisTypes.OrderBy(m => m.TypeName);
            ViewBag.MarkType = new SelectList(thesisType, "TypeName", "TypeName");
            return View();
        } 

        //
        // POST: /Markings/Create

        [HttpPost]
        public ActionResult Create(Marking marking)
        {
            if (ModelState.IsValid)
            {
                db.Markings.Add(marking);
                db.SaveChanges();
                return RedirectToAction("CreateTarget", new { id=marking.MarkingID});  
            }

            return View(marking);
        }

        #region 添加指标

        public ActionResult CreateTarget(int id)
        {
            
           

          //return View(targets.topToPagedList(pageIndex, pageSize));
          var targets = db.Targets
              .Include(m => m.Markings)
              .OrderByDescending(m => m.TargetID);
  
          ViewBag.markid = id;
            return View(targets);
        }
        [HttpPost]
        public ActionResult CreateTarget(string fileUrlList,int id,int[] targetID)
        {
            Marking marking = db.Markings.Find(id);
            if (targetID != null)
            {
                foreach (var item in targetID)
                {

                 
                    marking.Targets.Add(db.Targets.Find(item));
                }
                db.SaveChanges();
            }
            return RedirectToAction("Details");
        }
        #endregion

        public ActionResult Search(int? page, TargetsSearch targetsSearch)
        {
            var thesisType = db.ThesisTypes.ToList();
            ViewBag.TypeID = new SelectList(thesisType, "TypeID", "TypeName");

            var marking = db.Markings.ToList();
                //.Include(m=>m.Thesistypes)
                //.Include(m => m.Targets).ToList();

            if (!string.IsNullOrEmpty(targetsSearch.Title))
            {
                marking = marking.Where(m => m.Title.ToUpper().Contains(targetsSearch.Title.ToUpper())).ToList();
            }
            if (!string.IsNullOrEmpty(targetsSearch.MarkType))
            {
                marking = marking.Where(m => m.MarkType == targetsSearch.MarkType).ToList();
            }
            ViewBag.Current = targetsSearch;
            int pageSize =5;//定义每页显示的记录数
            int pageIndex = (page ?? 1) - 1;
            //return View( targets.topToPagedList(pageIndex, pageSize));
            return View(marking.ToPagedList(pageIndex, pageSize));
        }
        
        //
        // GET: /Markings/Edit/5
 
        public ActionResult Edit(int id)
        {
            Marking marking = db.Markings.Find(id);
            ViewBag.MarkType = new SelectList(db.ThesisTypes,"TypeName","TypeName",marking.MarkType);
            return View(marking);
        }

        //
        // POST: /Markings/Edit/5

        [HttpPost]
        public ActionResult Edit(Marking marking)
        {
            if (ModelState.IsValid)
            {
                db.Entry(marking).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(marking);
        }

        //
        // GET: /Markings/Delete/5
 
        public ActionResult Delete(int id)
        {
            Marking marking = db.Markings.Find(id);
            return View(marking);
        }

        //
        // POST: /Markings/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Marking marking = db.Markings.Find(id);
            db.Markings.Remove(marking);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}